//
//  WDWorderOrdMainVehOtherImageVideoView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/3.
//

#import "WDWorderOrdMainVehOtherImageVideoView.h"
#import "WDInspectUploadModel.h"
#import "HZPhotoBrowser.h"

@interface WDWorderOrdMainVehOtherImageVideoView ()
@property (nonatomic, copy) NSString *title;

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation WDWorderOrdMainVehOtherImageVideoView

- (void)initWithSubView {
    [super initWithSubView];
    self.backgroundColor = [UIColor whiteColor];
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(20, 0, self.width - 40, 18);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    self.titleLabel.text = objectNull(self.title);
}

- (void)updateWithViewTitle:(NSString *)title images:(NSArray *)images playVideoBlock:(void (^ _Nullable)(WDInspectUploadModel * _Nonnull))playVideoBlock {

//    [UIView animateWithDuration:1 animations:nil completion:nil];
    if (playVideoBlock) {
        self.touchPlayVideoBlock = playVideoBlock;
    }
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:images];
    self.title = title;
    
    [self initWithTitleLabel];
    @weakify(self);
    float leftSpace = 20;
    float midSpace = 16;
    float imageWidth = (kScreenW - 20 * 2 - 16 * 2) / 3.0;
    float imageHeight = imageWidth / 1.42857;
    CGRect frame = CGRectMake(leftSpace, self.titleLabel.bottom + 12, imageWidth, imageHeight);
    UIImageView *preView = nil;
    for (int i=0; i<self.dataList.count; i++) {
        WDInspectUploadModel *model = self.dataList[i];
        if (i % 3 == 0) {
            if (preView != nil) {
                frame.origin.x = leftSpace;
                frame.origin.y = preView.bottom + 10;
            }
        } else {
            frame.origin.x = preView.right + midSpace;
        }
        UIImageView *imgView = [UIImageView createWithImageView:self frame:frame defaultImage:nil propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.layer.cornerRadius = 5;
            imageView.layer.masksToBounds = YES;
//            imageView.backgroundColor = [UIColor colorWithRandom];
            imageView.userInteractionEnabled = YES;
        }];
        if (model.mediaType == 0) { // 图片
            [imgView sd_setImageWithURL:[NSURL URLWithString:model.uploadUrl] placeholderImage:nil];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
                @strongify(self);
                [self browseImageView:i view:imgView];
            }];
            [imgView addGestureRecognizer:tap];
        } else { // 视频
            [EDSUtility videoImageWithVideoURL:[NSURL URLWithString:model.uploadUrl] imgView:imgView];
            imgView.userInteractionEnabled = YES;
            UIImage *image = [UIImage imageNamed:@"com_play_small_ic"];
            CGRect frame = CGRectMake((imgView.width - image.size.width) * 0.5, (imgView.height - image.size.height) * 0.5, image.size.width, image.size.height);
            UIButton *btnPlayVideo = [UIButton createWithButton:imgView frame:frame font:FONTSIZE(16) target:self action:@selector(clickedPlay:) propertiesBlock:^(UIButton * _Nonnull button) {
                [button setImage:image forState:UIControlStateNormal];
            }];
            btnPlayVideo.tag = 100 + i;
        }
        
        [self.viewList addObject:imgView];
        preView = imgView;
    }
    
}

/// 播放视频
/// - Parameter sender: sender description
- (void)clickedPlay:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSInteger tag = btn.tag - 100;
    WDInspectUploadModel *model = self.dataList[tag];
//    debugLog(@"toubl=%@", self.touchPlayVideoBlock);
    
    NSDictionary *params = @{@"videoUrl":[NSURL URLWithString:model.uploadUrl]};
    UIViewController *vc = [[TYZRouter sharedInstance] getCommunityPlayerVC:params];
    [TYZJumpVCManager pushBaseVC:[EDSUtility cl_currentViewController] pushVC:vc];
//    if (self.touchPlayVideoBlock) {
//        self.touchPlayVideoBlock(model);
//    }
}

- (void)browseImageView:(NSInteger)index view:(UIImageView *)view {
    
    NSMutableArray *addList = [NSMutableArray new];
    for (WDInspectUploadModel *ent in self.dataList) {
        if (ent.mediaType == 0) {
            [addList addObject:ent.uploadUrl];
        }
    }
    
    // 图片预览
    HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
    browser.isNeedLandscape = NO;
    browser.imageArray = [addList copy];
    browser.currentImageIndex = (int)index;
    browser.imageCount = addList.count;
    if(addList.count == 1)//如果是一张图片tag 是200
    {
        browser.sourceImagesContainerView = view;
        browser.startTag = 200;
    }
    else//如果是多张图片tag 从100开始
    {
        browser.startTag = 100;
        browser.sourceImagesContainerView = view;
    }
    [browser show];
}

@end
